using System;
using System.Collections;
using System.Data;
using BO = PHSRAG.Insight21.BusinessObjects;
using DataAccess = PHSRAG.Utility.DataAccess;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using Utility = PHSRAG.Utility;

namespace PHSRAG.Insight21.CDS
{
	/// <summary>
	/// Represents a degree held by a Person
	/// </summary>
	public class Degree: BO.SmartBusinessObject
	{
		#region instance variables
		private string name = string.Empty;
		private string description = string.Empty;
		#endregion

		#region Constants
		private const string DegreeKey = "Degree_AD250C0F-6ED9-4d53-86D1-C736B36D56DF";
		private const string ColumnNameId = "Id";
		private const string ColumnNameName = "Name";
		private const string ColumnNameDescription = "Description";
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the name of the file (attachment) including the extension
		/// </summary>
		public string Name
		{
			get	{ return name;	}
			set { SetDirty(ref name, value); }
		}

		
		/// <summary>
		/// Gets or sets the date on which the file was created
		/// </summary>
		public string Description
		{
			get	{ return description; }
			set	{ SetDirty(ref description, value);	}
		}

		/// <summary>
		/// Cache Key uniquely identifies this object within the cache.
		/// </summary>
		/// <returns>the unique string for this object.</returns>
		public override string CacheKey()
		{
			return DegreeKey;
		}

		#endregion

		#region Constructors
		/// <summary>
		/// 
		/// </summary>
		/// <param name="cachePolicy"></param>
		public Degree(ICachePolicy cachePolicy): base(cachePolicy)
		{
			
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="cachePolicy"></param>
		/// <param name="dataRow"></param>
		public Degree(ICachePolicy cachePolicy,DataRow dataRow): base(cachePolicy)
		{
			PopulateMembers(dataRow);
		}
		#endregion

		#region public methods
		/// <summary>
		/// 
		/// </summary>
		/// <param name="personId"></param>
		/// <param name="cachePolicy"></param>
		/// <returns></returns>
		public static ArrayList GetDegrees(int personId,ICachePolicy cachePolicy)
		{
			ArrayList degreeArray=null;
			try
			{
				DataTable dtDegree = Utility.DataAccess.GetDataTable(Utility.DataAccess.GetConnectionString(),"GetDegreeForPerson"
					,Utility.DataAccess.BuildSqlParameterArray("@personId", SqlDbType.Int, personId));

				if (dtDegree !=null)
				{
					degreeArray=new ArrayList();
					Degree degree=null;
					foreach (DataRow dataRow in dtDegree.Rows)
					{
						degree=new Degree(cachePolicy);
						degree.PopulateMembers(dataRow);
						degreeArray.Add(degree);
					}
				}
				return degreeArray;
			}
			catch(Exception ex)
			{
				Utility.ExceptionReport.WriteToEventLog ("Degree.GetDegree: Failed to load the Degrees for a person", ex, true, Utility.ExceptionReport.ReportType.Error);
				throw;
			}


		}
		#region public override methods
		/// <summary>
		/// Determines if the object is dirty based on whether or not atleast one of its properties have changed
		/// </summary>
		/// <returns>true - if the object is dirty, false - if not.</returns>
		public override bool IsDirty()
		{
			return dirty;
		}

		/// <summary>
		/// Loads the Degree object from the database with the specified id.-Method not implemented(will throw exception if used) 
		/// </summary>
		/// <param name="connectionString">Information to connect to the database</param>
		/// <param name="args">Degree Id of the study person to be loaded</param>
		public override void Load(string connectionString, params object[] args)
		{
			throw new Exception("Load() not implemeneted");
		}

		/// <summary>
		/// Saves the instance of the Degree object to the database. -Method not implemented(will throw exception if used) 
		/// The stored procedure inserts a new record if the id of the object is 0, 
		/// otherwise the procedure updates the record with the matching degree id
		/// </summary>
		/// <param name="sqlTransaction">The context in which the insert/update should be executed</param>
		protected override void SaveObject(System.Data.SqlClient.SqlTransaction sqlTransaction)
		{
			throw new Exception("SaveObject() not implemeneted");
		}


		/// <summary>
		/// Executes the DeleteDegree stored procedure passing in the degree id as the parameterMethod not implemented(will throw exception if used) 
		/// </summary>
		/// <param name="sqlTransaction">The transaction context in which the SQL statement is to be executed</param>
		protected override void DeleteObject(System.Data.SqlClient.SqlTransaction sqlTransaction)
		{
			throw new Exception("DeleteObject() not implemeneted");
		}
		
		/// <summary>
		/// Cache this instance using the prevalent cache policy.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache( CacheKey(), this);
		}

		
		/// <summary>
		/// Remove this instance from the cache using the prevalent cache policy.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache( CacheKey());
		}

		#endregion
		#endregion

		#region Private methods
		/// <summary>
		/// Populates instance fields with corresponding values from the data row
		/// </summary>
		/// <param name="dataRow">A DataRow object holding the values to be populated to the instance fields</param>
		private void PopulateMembers(DataRow dataRow)
		{
			Id = (int)dataRow[ColumnNameId];
			name = (string)dataRow[ColumnNameName];
			Description = (string)dataRow[ColumnNameDescription];
		}				
		#endregion
	}
}
